<style>
  .layui-layer-title {
    display: flex;
    align-items: center;
  }

  #warehouseComputerPractice-form .layui-disabled {
    color: #393d49 !important;
  }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
<!--                <div class="layui-inline">-->
<!--                  <label class="layui-form-label w-auto">车间</label>-->
<!--                  <div class="layui-input-inline mr0">-->
<!--                    <select id="workshopId" name="workshopId" lay-filter="workshopId" lay-search required></select>-->
<!--                  </div>-->
<!--                </div>-->
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">产线</label>
                  <div class="layui-input-inline mr0">
                    <select id="productionLineId" name="productionLineId" lay-filter="productionLineId" lay-search required></select>
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label">生产计划编号</label>
                  <div class="layui-input-inline mr0">
                    <input name="productionSchedulingPlanCode" class="layui-input" type="text" placeholder="请输入生产计划编号" />
                  </div>
                </div>
                  <div class="layui-inline">
                      <label class="layui-form-label">客户名称</label>
                      <div class="layui-input-inline mr0">
                          <input name="customerName" class="layui-input" type="text"
                                 placeholder="请输入客户名称"/>
                      </div>
                  </div>
                  <div class="layui-inline">
                      <label class="layui-form-label">捆包号</label>
                      <div class="layui-input-inline mr0">
                          <input name="userBundleCode" class="layui-input" type="text"
                                 placeholder="请输入捆包号"/>
                      </div>
                  </div>
                <div class="layui-inline">
                  <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                  <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button id="warehouseComputerPracticeBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
            </div>
          </div>
        </div>
      </div>

      <!-- 表格 -->
      <table class="layui-table" id="warehouseComputerPracticeTable" lay-filter="warehouseComputerPracticeTable"></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="warehouseComputerPracticeTableBar">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  <a class="layui-btn layui-btn-xs layui-btn" lay-event="detail">详情</a>
  {{# if(d.status === '1'){ }}
  <a class="layui-btn layui-btn-xs layui-btn" lay-event="AddProcessing">报工</a>
  {{# } }}
</script>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "util", "table", "admin", "laytpl", "laydate", "tableTreeDj"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;

    // 初始化车间下拉框
    admin.initDataSelectPro(
            "warehouse/warehouse/productionLine/list?page=1&limit=9999",  // 替换为实际请求地址
            { limit: 9999 }, // 参数
            "workshopId",  // 目标下拉框ID
            "客户",  // 下拉框标题
            async (d, obj) => { },
            "workshopName"  // 使用 workshopName 作为显示文本
    );

    // 初始化产线下拉框
    admin.initDataSelectPro(
            "warehouse/warehouse/productionLine/list?page=1&limit=9999",  // 替换为实际请求地址
            { limit: 9999 }, // 参数
            "productionLineId",  // 目标下拉框ID
            "产线",  // 下拉框标题
            async (d, obj) => { },
            "productionLineName"  // 使用 productionLineName 作为显示文本
    );

    // 渲染表格
    var insTb = table.render({
      elem: "#warehouseComputerPracticeTable",
      url: config.base + "warehouse/warehouse/computerPractice/list",
      toolbar: true,
      defaultToolbar: ["filter"],
      where: { access_token: config.getToken().access_token },
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          { type: "numbers" },
          { field: "computerPracticeCode", align: "center", title: "上机记录编号" },
          { field: "productionSchedulingPlanDetailCode", align: "center", title: "制令单编号" },
          { field: "productionPlanCode", align: "center", title: "生产计划编号" },
          { field: "plannedQuantity", align: "center", title: "计划生产数量" },
            {field: "customerName", align: "center", title: "客户名称"},
            {field: "userBundleCode", align: "center", title: "捆包号"},
          { field: "completedQuantity", align: "center", title: "已完成数量" },
          { field: "productionLineName", align: "center", title: "生产线" },
          // { field: "workshopName", align: "center", title: "所属车间" },
          { field: "materialNumber", align: "center", title: "材料料号" },
          // { field: "userBundleCode", align: "center", title: "捆包码" },
          { field: "sysBundleCode", align: "center", title: "唯一码" },
          { field: "creatorName", align: "center", title: "操作人" },
          { field: "createTime", align: "center", title: "操作时间" },
          {
            field: "status",
            align: "center",
            title: "状态",
            templet: function (d) {
              return d.status == 1 ? "待报工" : "已报工";
            }
          },
          { align: "center",fixed: "right", toolbar: "#warehouseComputerPracticeTableBar", fixed: "right",title: "操作", width: 240 }
        ]
      ]
    });

    // 工具条点击事件
    table.on("tool(warehouseComputerPracticeTable)", function (obj) {
      var data = obj.data;
      var layEvent = obj.event;

      if (layEvent === "detail") {
        admin.req(
                "warehouse/warehouse/computerPractice/selectComputerPractice",
                { id: data.id },
                (res) => {
                  if (res.code === 200) {
                    openFormFun("detail", res.data);
                  }
                }
        );
      } else if (layEvent === "edit") {
        admin.req(
                "warehouse/warehouse/computerPractice/selectComputerPractice",
                { id: data.id },
                (res) => {
                  if (res.code === 200) {
                    openFormFun("edit", res.data);
                  }
                }
        );
      }else if(layEvent==='AddProcessing'){
          admin.formOpen({
              area: "90%",
              offset: "5%",
              title: "添加加工报工记录",
              path: "components/warehouse/product/warehouseProcessingForm.html",
              success: function () {
                  parentData.eventName = 'add';
                  parentData.computerPracticeCodeI = data.id;
              }
          });
      } else if (layEvent === "del") {
        // 确认是否删除
        layer.open({
          title: "提示",
          content: "是否删除该记录？",
          btn: ["确认删除", "关闭"],
          yes: function (index) {
            admin.req("warehouse/warehouse/computerPractice/deleteComputerPractice", { id: data.id }, (res) => {
              if (res.code === 200) {
                layer.msg("删除成功");
                layer.close(index);
                insTb.reload();
              } else {
                layer.msg(res.msg);
              }
            });
          }
        });
      }
    });

    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", function (data) {
      insTb.reload({ where: admin.resetSearch(data.field), page: { curr: 1 } }, "data");
    });

    form.render("select");

    // 添加按钮点击事件
    $("#warehouseComputerPracticeBtnAdd").click(function () {
      openFormFun("add");
    });

    function openFormFun(eventName, data) {
      admin.formOpen({
        area: "90%",
        offset: "5%",
        title: "配置订单物料信息",
        path: "components/warehouse/product/warehouseComputerPracticeForm.html",
        success: function () {
          parentData.eventName = eventName;
          parentData.data = data;
        }
      });
    }
  });
</script>
